/*
 *  Cero Project - Copyright   2006 The Cero Developement Team 
 *  (Michael Laguerre, Camille Roux, Matthieu Segret, Mathieu Sivade) 
 * 
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the Free 
 *  Software Foundation; either version 2 of the License, or (at your option) 
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 */

package org.ceroproject.games.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.ceroproject.Configurable;
import org.ceroproject.ConfigurationException;
import org.ceroproject.Configurer;
import org.ceroproject.games.Action;
import org.ceroproject.games.Player;
import org.ceroproject.plugin.PluginManager;
import org.ceroproject.ui.ChoiceMaker;
import org.ceroproject.ui.UserInterface;


/**
 * Basic implementation of Player. Extend it into another class to have a working player.
 * 
 * @author Telem
 */
public abstract class PlayerBase implements Player, Configurable {

	/** The id which will be given to the next card */
	private static int currentID = 0;

	/**
	 * Returns and increments the unique player ID
	 * 
	 * @return an unique player ID
	 */
	private int nextID() {
		return currentID++;
	}

	/** This player's ID */
	private int playerID;

	/** This player's name */
	private String playerName;

	private MyConfigurer myConf;

	/** Zones the player owns */
	private ZonesBase zones = new ZonesBase();

	/** Zones the player owns */
	private List<Action> actions = new ArrayList<Action>();

	/**
	 * Creates a player, which name must be set using the Configurable
	 * properties
	 * 
	 */
	public PlayerBase() {
		playerID = nextID();
		myConf = new MyConfigurer();
	}

	public int getPlayerId() {
		return playerID;
	}

	public String getPlayerName() {
		return playerName;
	}
	
	public void setPlayerName(String name) {
		playerName = name;
	}

	public ZonesBase getZones() {
		return zones;
	}

	public Collection<Action> getActions() {
		return actions;
	}

	public String toString() {
		return playerName;
	}

	public Configurer getConfiguration() {
		return myConf;
	}

	public Configurer getDefaultConfiguration() {
		return new MyConfigurer();
	}

	public ChoiceMaker getChoiceMaker() {
		//FIXME this is quite ugly, it needs to be made not referencing the ui package somehow
		for (UserInterface ui : PluginManager.getInstance()
				.getInterfaceInstances()) {
			if (ui != null && ui.isActivated()) {
				try {
					return ui.getChoiceMaker();
				} catch (UnsupportedOperationException e) {
				}
			}
		}
		return null;
	}

	private class MyConfigurer extends Configurer {

		public MyConfigurer() {
			super(String.valueOf(playerID));
			try {
				setString("name", "Player" + playerID);
			} catch (Exception e) {
				// there is no reason it would crash here
				// though, printing doesn't hurt
				e.printStackTrace();
			}
		}

		@Override
		public void setString(String propertyName, String value)
				throws ConfigurationException {
			if (propertyName.equals("name")
					&& (value.equals("") || value == null))
				throw new ConfigurationException(
						"Players' name can't be null or empty");
			playerName = value;
			super.setString(propertyName, value);
		}

	}
}
